<template>
	<view class="game-container">
		<view class="game-header">
			<text class="player-name">饼干大师</text>
			<text class="cookie-count">{{Math.floor(cookies)}} 块饼干</text>
			<text class="cps-count">每秒: {{cookiesPerSecond.toFixed(1)}}</text>
		</view>
		
		<view class="main-content">
			<view class="cookie-section">
				<view class="cookie-wrapper">
					<view class="cookie-container" :class="{'cookie-click': isClicking}">
						<view class="cookie-shadow"></view>
						<view class="cookie-rays"></view>
						<image 
							src="https://orteil.dashnet.org/cookieclicker/img/perfectCookie.png" 
							class="cookie-image"
							@click="clickCookie"
							:style="{ transform: `scale(${isClicking ? 0.95 : 1}) rotate(${rotation}deg)` }"
						/>
						<view class="cookie-shine"></view>
						
						<view v-for="(cursor, index) in cursorElements" 
							:key="cursor.id"
							class="cursor-element"
							:style="cursor.style">
							<view class="cursor-click-effect"></view>
						</view>
						
						<view class="grandmas-container">
							<view v-for="(grandma, index) in grandmaElements" 
								:key="grandma.id"
								class="grandma-element"
								:style="grandma.style">
								<view class="grandma-thought" v-if="index % 3 === 0">🍪...</view>
							</view>
						</view>
					</view>
					
					<view v-for="particle in clickParticles" 
						:key="particle.id" 
						class="click-particle"
						:style="particle.style">
						<text class="particle-text">+{{particle.value}}</text>
					</view>
				</view>
			</view>

			<view class="store-section">
				<view class="store-header">
					<text class="store-title">商店</text>
				</view>
				<view class="store-items">
					<view class="store-item" 
						:class="{'can-buy': cookies >= cursorCost}"
						@click="buyCursor">
						<view class="item-icon cursor-icon"></view>
						<view class="item-info">
							<text class="item-name">自动点击器</text>
							<text class="item-cost">{{Math.floor(cursorCost)}} 饼干</text>
							<text class="item-owned">拥有: {{cursors}}</text>
							<text class="item-cps">每个产量: 0.1/秒</text>
						</view>
					</view>
					<view class="store-item" 
						:class="{'can-buy': cookies >= grandmaCost}"
						@click="buyGrandma">
						<view class="item-icon grandma-icon"></view>
						<view class="item-info">
							<text class="item-name">饼干奶奶</text>
							<text class="item-cost">{{Math.floor(grandmaCost)}} 饼干</text>
							<text class="item-owned">拥有: {{grandmas}}</text>
							<text class="item-cps">每个产量: 1.0/秒</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			cookies: 0,
			cursors: 0,
			grandmas: 0,
			cursorCost: 15,
			grandmaCost: 100,
			cookiesPerSecond: 0,
			isClicking: false,
			clickParticles: [],
			particleId: 0,
			rotation: 0,
			lastClickTime: 0,
			clickCombo: 1,
			clickTimer: null,
			autoSaveTimer: null,
			cursorElements: [],
			grandmaElements: [],
			nextCursorId: 0,
			nextGrandmaId: 0,
			productionInterval: null,
			cursorInterval: null,
			cursorRotation: 0,
			cursorRotationInterval: null
		}
	},
	methods: {
		clickCookie() {
			const now = Date.now();
			if (now - this.lastClickTime < 300) {
				this.clickCombo = Math.min(this.clickCombo + 1, 5);
			} else {
				this.clickCombo = 1;
			}
			this.lastClickTime = now;
			
			this.cookies += this.clickCombo;
			this.isClicking = true;
			this.createClickParticle(this.clickCombo);
			this.rotation += (Math.random() - 0.5) * 10;
			
			if (this.clickTimer) {
				clearTimeout(this.clickTimer);
			}
			this.clickTimer = setTimeout(() => {
				this.isClicking = false;
			}, 50);
		},
		createClickParticle(value) {
			const angle = Math.random() * Math.PI * 2;
			const distance = 60 + Math.random() * 40;
			const x = Math.cos(angle) * distance;
			const y = Math.sin(angle) * distance;
			
			const particle = {
				id: this.particleId++,
				value: value,
				style: {
					left: '50%',
					top: '50%',
					transform: `translate(${x}rpx, ${y}rpx)`,
					opacity: 1
				}
			};
			
			this.clickParticles.push(particle);
			setTimeout(() => {
				const index = this.clickParticles.findIndex(p => p.id === particle.id);
				if (index !== -1) {
					this.clickParticles.splice(index, 1);
				}
			}, 1000);
		},
		updateCursorPositions() {
			const radius = 120;
			const positions = [];
			for (let i = 0; i < this.cursors; i++) {
				const angle = (i / this.cursors) * Math.PI * 2 + (this.cursorRotation * Math.PI / 180);
				const x = Math.cos(angle) * radius;
				const y = Math.sin(angle) * radius;
				positions.push({
					id: i,
					angle: angle,
					style: {
						transform: `translate(${x}rpx, ${y}rpx) rotate(${angle * 180 / Math.PI + 90}deg)`
					}
				});
			}
			this.cursorElements = positions;
		},
		updateGrandmaPositions() {
			const radius = 180;
			const positions = [];
			for (let i = 0; i < this.grandmas; i++) {
				const angle = (i / this.grandmas) * Math.PI * 2;
				const x = Math.cos(angle) * radius;
				const y = Math.sin(angle) * radius;
				positions.push({
					id: i,
					style: {
						transform: `translate(${x}rpx, ${y}rpx)`
					}
				});
			}
			this.grandmaElements = positions;
		},
		startAutoProduction() {
			// 清除旧的定时器
			if (this.productionInterval) {
				clearInterval(this.productionInterval);
			}
			if (this.cursorInterval) {
				clearInterval(this.cursorInterval);
			}
			if (this.cursorRotationInterval) {
				clearInterval(this.cursorRotationInterval);
			}
			
			// 自动点击器旋转动画
			this.cursorRotationInterval = setInterval(() => {
				this.cursorRotation += 0.5;
				this.updateCursorPositions();
			}, 50);
			
			// 老奶奶生产
			this.productionInterval = setInterval(() => {
				if (this.grandmas > 0) {
					this.cookies += (this.grandmas * 1) / 10;
					// 随机选择一个老奶奶显示生产效果
					const randomIndex = Math.floor(Math.random() * this.grandmas);
					if (this.grandmaElements[randomIndex]) {
						const grandma = this.grandmaElements[randomIndex];
						this.createProductionParticle(grandma);
					}
				}
			}, 100);
			
			// 自动点击器点击
			this.cursorInterval = setInterval(() => {
				if (this.cursors > 0) {
					this.cookies += this.cursors * 0.1;
					// 随机选择一个点击器显示点击效果
					const randomIndex = Math.floor(Math.random() * this.cursors);
					if (this.cursorElements[randomIndex]) {
						const cursor = this.cursorElements[randomIndex];
						this.createCursorClickEffect(cursor);
					}
				}
			}, 1000);
		},
		buyCursor() {
			if (this.cookies >= this.cursorCost) {
				this.cookies -= this.cursorCost;
				this.cursors++;
				this.cursorCost = Math.floor(this.cursorCost * 1.15);
				this.updateCookiesPerSecond();
				this.updateCursorPositions();
			}
		},
		buyGrandma() {
			if (this.cookies >= this.grandmaCost) {
				this.cookies -= this.grandmaCost;
				this.grandmas++;
				this.grandmaCost = Math.floor(this.grandmaCost * 1.15);
				this.updateCookiesPerSecond();
				this.updateGrandmaPositions();
			}
		},
		updateCookiesPerSecond() {
			this.cookiesPerSecond = this.cursors * 0.1 + this.grandmas * 1;
		},
		saveGame() {
			try {
				const gameData = {
					cookies: this.cookies,
					cursors: this.cursors,
					grandmas: this.grandmas,
					cursorCost: this.cursorCost,
					grandmaCost: this.grandmaCost,
					cookiesPerSecond: this.cookiesPerSecond
				};
				uni.setStorageSync('cookieClickerSave', gameData);
			} catch (e) {
				console.error('保存游戏失败:', e);
			}
		},
		loadGame() {
			try {
				const savedData = uni.getStorageSync('cookieClickerSave');
				if (savedData) {
					Object.assign(this, savedData);
					this.updateCursorPositions();
					this.updateGrandmaPositions();
				}
			} catch (e) {
				console.error('加载游戏失败:', e);
			}
		},
		createCursorClickEffect(cursor) {
			const angle = cursor.angle;
			const radius = 120;
			const x = Math.cos(angle) * radius;
			const y = Math.sin(angle) * radius;
			
			const particle = {
				id: this.particleId++,
				value: 0.1,
				style: {
					left: '50%',
					top: '50%',
					transform: `translate(${x}rpx, ${y}rpx)`,
					opacity: 1,
					color: '#aaf'
				}
			};
			
			this.clickParticles.push(particle);
			setTimeout(() => {
				const index = this.clickParticles.findIndex(p => p.id === particle.id);
				if (index !== -1) {
					this.clickParticles.splice(index, 1);
				}
			}, 1000);
		},
		createProductionParticle(grandma) {
			const style = grandma.style;
			const match = style.transform.match(/translate\((.*?)rpx,\s*(.*?)rpx\)/);
			if (match) {
				const x = parseFloat(match[1]);
				const y = parseFloat(match[2]);
				
				const particle = {
					id: this.particleId++,
					value: 0.1,
					style: {
						left: '50%',
						top: '50%',
						transform: `translate(${x}rpx, ${y}rpx)`,
						opacity: 1,
						color: '#ffb'
					}
				};
				
				this.clickParticles.push(particle);
				setTimeout(() => {
					const index = this.clickParticles.findIndex(p => p.id === particle.id);
					if (index !== -1) {
						this.clickParticles.splice(index, 1);
					}
				}, 1000);
			}
		}
	},
	mounted() {
		this.loadGame();
		this.startAutoProduction();
		this.autoSaveTimer = setInterval(() => {
			this.saveGame();
		}, 30000);
	},
	beforeDestroy() {
		if (this.autoSaveTimer) {
			clearInterval(this.autoSaveTimer);
		}
		if (this.productionInterval) {
			clearInterval(this.productionInterval);
		}
		if (this.cursorInterval) {
			clearInterval(this.cursorInterval);
		}
		if (this.cursorRotationInterval) {
			clearInterval(this.cursorRotationInterval);
		}
		this.saveGame();
	}
}
</script>

<style>
.game-container {
	min-height: 100vh;
	background: #1a1a24;
	color: #fff;
	display: flex;
	flex-direction: column;
}

.game-header {
	background: #23233c;
	padding: 20rpx 40rpx;
	border-bottom: 4rpx solid #000;
	text-align: center;
}

.player-name {
	font-size: 36rpx;
	color: #fff;
	margin-bottom: 10rpx;
	display: block;
}

.cookie-count {
	font-size: 48rpx;
	color: #fff;
	text-shadow: 0 0 10rpx #4af;
	margin-bottom: 5rpx;
	display: block;
}

.cps-count {
	font-size: 24rpx;
	color: #9ab;
	display: block;
}

.main-content {
	display: flex;
	flex: 1;
}

.cookie-section {
	flex: 2;
	background: #15152b;
	display: flex;
	justify-content: center;
	align-items: center;
	position: relative;
	overflow: hidden;
}

.cookie-rays {
	position: absolute;
	width: 600rpx;
	height: 600rpx;
	background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
	animation: rays 8s linear infinite;
}

@keyframes rays {
	from { transform: rotate(0deg); }
	to { transform: rotate(360deg); }
}

.cookie-wrapper {
	position: relative;
	width: 400rpx;
	height: 400rpx;
}

.cookie-container {
	position: relative;
	width: 300rpx;
	height: 300rpx;
	margin: 50rpx;
	cursor: pointer;
}

.cookie-shadow {
	position: absolute;
	width: 100%;
	height: 100%;
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.5);
	filter: blur(20rpx);
	transform: translateY(20rpx);
	transition: all 0.05s ease;
}

.cookie-image {
	position: relative;
	width: 100%;
	height: 100%;
	transition: all 0.1s cubic-bezier(0.18, 0.89, 0.32, 1.28);
	will-change: transform;
	filter: drop-shadow(0 0 30rpx rgba(255,255,255,0.2));
}

.cookie-shine {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	border-radius: 50%;
	background: linear-gradient(135deg, rgba(255,255,255,0.4) 0%, rgba(255,255,255,0) 50%);
	pointer-events: none;
	opacity: 0;
	transition: opacity 0.2s ease;
}

.cookie-click .cookie-shadow {
	transform: translateY(10rpx) scale(0.95);
	opacity: 0.3;
}

.click-particle {
	position: absolute;
	pointer-events: none;
	z-index: 10;
	transition: all 1s ease-out;
}

.particle-text {
	font-size: 32rpx;
	font-weight: bold;
	color: #4af;
	text-shadow: 0 0 10rpx rgba(68,170,255,0.5);
	white-space: nowrap;
	opacity: 1;
	animation: particleFade 1s ease-out forwards;
}

@keyframes particleFade {
	0% { 
		transform: translateY(0) scale(1);
		opacity: 1;
	}
	100% { 
		transform: translateY(-50rpx) scale(0.5);
		opacity: 0;
	}
}

.store-section {
	flex: 1;
	background: #23233c;
	border-left: 4rpx solid #000;
	display: flex;
	flex-direction: column;
	min-width: 300rpx;
}

.store-header {
	padding: 20rpx;
	background: #2a2a45;
	border-bottom: 4rpx solid #000;
}

.store-title {
	font-size: 36rpx;
	color: #fff;
	text-align: center;
	text-shadow: 0 0 10rpx rgba(68,170,255,0.3);
}

.store-items {
	flex: 1;
	overflow-y: auto;
	padding: 20rpx;
}

.store-item {
	display: flex;
	padding: 20rpx;
	margin-bottom: 20rpx;
	background: #2a2a45;
	border: 2rpx solid #000;
	border-radius: 10rpx;
	opacity: 0.7;
	transition: all 0.3s ease;
}

.store-item.can-buy {
	opacity: 1;
	background: #32324f;
	cursor: pointer;
	box-shadow: 0 0 20rpx rgba(68,170,255,0.2);
}

.store-item.can-buy:active {
	transform: scale(0.98);
}

.item-icon {
	width: 80rpx;
	height: 80rpx;
	margin-right: 20rpx;
	background: #444;
	border-radius: 10rpx;
}

.cursor-icon {
	background: url('') center/60% no-repeat #444;
}

.grandma-icon {
	background: url('') center/60% no-repeat #444;
}

.item-info {
	flex: 1;
	display: flex;
	flex-direction: column;
}

.item-name {
	font-size: 28rpx;
	color: #fff;
	margin-bottom: 5rpx;
}

.item-cost {
	font-size: 24rpx;
	color: #4af;
}

.item-owned {
	font-size: 22rpx;
	color: #9ab;
	margin-top: 5rpx;
}

.item-cps {
	font-size: 20rpx;
	color: #678;
}

@keyframes float {
	0%, 100% { transform: translateY(0); }
	50% { transform: translateY(-10rpx); }
}

.cookie-container {
	animation: float 3s ease-in-out infinite;
}

.cursor-element {
	position: absolute;
	width: 24rpx;
	height: 24rpx;
	left: 50%;
	top: 50%;
	pointer-events: none;
	z-index: 5;
	transform-origin: center;
	will-change: transform;
	transition: transform 0.05s linear;
}

.cursor-click-effect {
	width: 100%;
	height: 100%;
	background: url('') center/contain no-repeat;
	filter: drop-shadow(0 0 5rpx rgba(255,255,255,0.5));
	animation: cursorPulse 0.5s ease-out;
}

@keyframes cursorPulse {
	0% { transform: scale(1); }
	50% { transform: scale(1.2); }
	100% { transform: scale(1); }
}

.grandmas-container {
	position: absolute;
	width: 100%;
	height: 100%;
	pointer-events: none;
	z-index: 4;
}

.grandma-element {
	position: absolute;
	width: 40rpx;
	height: 40rpx;
	left: 50%;
	top: 50%;
	pointer-events: none;
	z-index: 4;
	transform-origin: center;
	will-change: transform;
	transition: transform 0.3s ease-out;
	background: url('') center/contain no-repeat;
	filter: drop-shadow(0 0 8rpx rgba(255,255,255,0.4));
	animation: grandmaBake 2s ease-in-out infinite;
}

@keyframes grandmaBake {
	0%, 100% { transform: rotate(-5deg); }
	50% { transform: rotate(5deg); }
}

.grandma-thought {
	position: absolute;
	top: -25rpx;
	left: 50%;
	transform: translateX(-50%);
	font-size: 20rpx;
	color: #ffb;
	text-shadow: 0 0 5rpx rgba(255,255,255,0.5);
	white-space: nowrap;
}
</style> 